package com.kersci.heartratereader

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView
import com.kersci.heartratereader.utils.DisplayUtil


class AutoHideBottomUIDialog : Dialog {
    private var mContext: Context = context
    lateinit var tvScanStatus :TextView
    lateinit var tvDeviceFound : EditText

    constructor(context: Context) : super(context)

    private var height = 0
    private var width = 0

    override fun onStart() {
        super.onStart()
        window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window?.decorView?.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
    }

    override fun show() {
        this.window?.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        this.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)

        var window = this.window
        window?.attributes?.height = height
        window?.attributes?.width = width
        window?.setBackgroundDrawable(mContext.resources.getDrawable(R.color.color_transparent))
        window?.setGravity(Gravity.CENTER)
        tvScanStatus = window?.findViewById(R.id.tvScanStatus)!!
        tvDeviceFound = window?.findViewById(R.id.tvDeviceFound)!!
        if (tvDeviceFound != null) {
            tvDeviceFound.isEnabled = false
        }
        super.show()
    }

    fun setDialogHeight(value : Int) : AutoHideBottomUIDialog {
        this.height = value
        return this
    }

    fun setDialogWidth(value : Int) : AutoHideBottomUIDialog {
        this.width = value
        return this
    }

    fun setScanStatus(status: String) {
        if (tvScanStatus != null && tvDeviceFound != null) {
            tvScanStatus.text = status
            var deviceManager = App.instance.getDeviceManager()
            var deviceFoundList = deviceManager.getDevices()
            if (deviceFoundList.isNotEmpty()) {
                tvDeviceFound.text.clear()

                for (device in deviceFoundList) {
                    var info = "${device.name}\n\tmac:${device.mac}\n\trssi:${device.rssi}\n"
                    tvDeviceFound.text.insert(0, info)
                }
            }
        }
    }
}